Page({
  data: {
    SDKVersion: '',
    timer: null as unknown as number,
    totpList: [
      {
        isExpanded: false,
        totpCode: '123456',
        countdown: 30,
        image: '/images/github.jpg',
        title: 'GitHub账号',
        description: '每30秒更新一次',
        date: '2024-03-20'
      },
      {
        isExpanded: false,
        totpCode: '654321',
        countdown: 30,
        image: '/images/gitee.jpg',
        title: '码云账号',
        description: '每60秒更新一次',
        date: '2024-03-19'
      },
      {
        isExpanded: false,
        totpCode: '987654',
        countdown: 30,
        image: '/images/deepseek.png',
        title: '深度求索',
        description: '动态口令验证',
        date: '2024-03-18'
      }
    ],
    rightBtns: [
      {
          text: '修改',
          bgColor: '#FFA91B',
          color: '#fff',
          slotName:'edit',
          width:80
      },
      {
          text: '删除',
          bgColor: '#FF2B00',
          color: '#fff',
          slotName:'delete',
          width:80
      }
    ],
    swipeIndex: -1,
  },

  onShow() {
    this.setData({ 
      SDKVersion: my.SDKVersion,
      timer: setInterval(() => this.updateCountdown(), 1000) as unknown as number
    });
  },

  onUnload() {
    clearInterval(this.data.timer);
  },

  handleItemTap(e) {
    //点击项目事件处理
    const index = e.currentTarget.dataset.index;
    const key = `totpList[${index}].isExpanded`;
    this.setData({ [key]: !this.data.totpList[index].isExpanded });
  },

  updateCountdown() {
    this.data.totpList.forEach((item, index) => {
      if (item.isExpanded) {
        const key = `totpList[${index}].countdown`;
        const newCount = item.countdown > 0 ? item.countdown - 1 : 30;
        this.setData({ [key]: newCount });
      }
    });
  },

  onTitleClick() {
    // 标题被点击
  },

  onPullDownRefresh() {
    // 页面被下拉
  },

  onReachBottom() {
    // 页面被拉到底部
  },

  onShareAppMessage() {
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index'
    };
  },
  onSwipeStart() {
    //清除滑动项目索引
    this.setData({ swipeIndex: -1 });
  },
  onSwipeEnd(args1, args2) {
      //设置滑动索引
      let e, data;
      e = args2;
      data = args1;
      const { index } = e.target.dataset.item;
      data.swiped && this.setData({ swipeIndex: index });
  },
  onButtonTap(data, e) {
      //点击了滑动出现的按钮事件
      console.log(data, e);
  }
});
